package com.mxpio.erp.common.quality.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.mxpio.erp.common.enums.InspectionEnums;
import com.mxpio.erp.common.jpa.model.Order;
import com.mxpio.erp.common.mes.entity.ManufactureWorkOrder;
import com.mxpio.erp.common.mes.entity.ManufactureWorkOrderTask;
import com.mxpioframework.security.annotation.Dict;
import com.mxpioframework.security.entity.User;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Author : wpp
 * @Datetime : 2023/6/6 14:32
 * @Desc :
 * @Modor :  Modifytime:
 * @modDesc : 质检单据
 */
@Data
@EqualsAndHashCode(callSuper=false)
@Entity
@Table(name = "MB_ERP_QUALITY_IB")
@Schema(description="质检单据")
@AllArgsConstructor
@NoArgsConstructor
public class InspectionBill extends Order {

    private static final long serialVersionUID = 1L;

    @Column(name = "BUSI_TYPE_")
    @Schema(description = "适用业务类型")
    @Dict(dicCode = "ERP_QUAL_IS_BUSI_TYPE")
    private String busiType;


    @Column(name = "CHECK_STATUS_")
    @Schema(description = "检验状态")
    @Dict(dicCode="ERP_QUAL_CHECK_STATUS")
    private String checkStatus;

    @Column(name = "APPLY_ORG_TYPE_")
    @Schema(description = "报检组织类别")
    @Dict(dicCode="ERP_QUAL_APPLY_ORG_TYPE")
    private String applyOrgType;

    @Column(name = "APPLY_ORG_NAME_")
    @Schema(description = "报检组织名称")
    private String applyOrgName;

    @Column(name = "APPLY_ORG_CODE_")
    @Schema(description = "报检组织编码")
    private String applyOrgCode;


    @Column(name = "ORIGIN_BIZ_TYPE_")
    @Schema(description = "来源单类型")
    @Dict(dicCode="ERP_QUAL_ORIGIN_BIZ_TYPE")
    private String originBizType;

    @Column(name = "ORIGIN_BIZ_LINE_NO_")
    @Schema(description = "来源业务单行号")
    private String originBizLineNo;

    @Column(name = "ORIGIN_BIZ_NO_")
    @Schema(description = "来源业务单号")
    private String originBizNo;

    @Column(name = "ORIGIN_NOTICE_LINE_NO_")
    @Schema(description = "来源验收单行号")
    private String originNoticeLineNo;

    @Column(name = "ORIGIN_NOTICE_NO_")
    @Schema(description = "来源验收单号")
    private String originNoticeNo;//来源

    @Column(name = "ORIGIN_WORK_ORDER_TASK_CODE_")
    @Schema(description = "工单工序任务编码")
    private String originWorkOrderTaskCode;

    @Column(name = "ORIGIN_WORK_ORDER_CODE_")
    @Schema(description = "工单（批次）编码")
    private String originWorkOrderCode;

    @Column(name = "ITEM_CODE_")
    @Schema(description = "物料代码")
    private String itemCode;

    @Column(name = "ITEM_NAME_")
    @Schema(description = "物料名称")
    private String itemName ;

    @Column(name = "ITEM_SPEC_")
    @Schema(description = "规格型号")
    private String itemSpec;

    @Column(name = "QUANTITY_")
    @Schema(description = "报检数量")//验收单确认数量
    private BigDecimal quantity;

    @Column(name = "UNIT_CODE_")
    @Schema(description = "单位编号")
    @Dict(dicCode="ERP_TECH_UNIT_CODE")
    private String unitCode;

    @Column(name = "DRAWING_NO_")
    @Schema(description = "图号")
    private String drawingNo;

    @Column(name = "TEST_RESULT_")
    @Schema(description = " 检验结论")
    @Dict(dicCode = "MB_QUALITY_CHECK_RESULT")//0不合格，1合格
    private String testResult;

    @Column(name = "CHECK_QUANTITY_")
    @Schema(description = "检验数量")
    private BigDecimal checkQuantity;

    @Column(name = "UNQUALIFIED_QUANTITY_")
    @Schema(description = "不合格数量")
    private BigDecimal unqualifiedQuantity;

    @Column(name = "CONCESSION_QUANTITY_")
    @Schema(description = "让步接收")
    private BigDecimal concessionQuantity;

    @Column(name = "REFUND_QUANTITY_")
    @Schema(description = "判退数量")
    private BigDecimal refundQuantity;

    @Column(name = "RECEIVE_QUANTITY_")
    @Schema(description = "合格数量")//接收数量通过汇总计算
    private BigDecimal receiveQuantity;

    @Column(name = "REPAIR_QUANTITY_")
    @Schema(description = "返修数量")
    private BigDecimal repairQuantity=BigDecimal.ZERO;//暂无对应具体逻辑，目前默认为0

    @Column(name = "REWORK_QUANTITY_")
    @Schema(description = "返工数量")
    private BigDecimal reworkQuantity=BigDecimal.ZERO;

    @Column(name = "SCRAP_QUANTITY_")
    @Schema(description = "报废数量")
    private BigDecimal scrapQuantity=BigDecimal.ZERO;


    @Column(name = "REVIEW_")
    @Schema(description = "是否不合格品处理评审")
    @Dict(dicCode = "ERP_COMMON_YESNO")
    private String review;

    @Column(name = "HANDLING_NO_")
    @Schema(description = "不合格品处理单号")
    private String handlingNo;

    @Column(name = "INSPECTOR_")
    @Schema(description = "检验员")
    @Dict(dicCode = "username", dicEntity = User.class, dicText = "nickname")
    private String inspector;

    @Column(name = "INSPECTION_START_TIME_")
    @Schema(description = "检验开始时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date inspectionStartTime;

    @Column(name = "INSPECTION_FINISH_TIME_")
    @Schema(description = "检验结束时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date inspectionFinishTime;

    @Column(name = "UNQUALIFIED_DESC_")
    @Schema(description = "不合格描述")
    private String unqualifiedDesc;

    @Column(name = "UNQUALIFIED_TYPE_")
    @Schema(description = "不合格原因类型")
    @Dict(dicCode = "ERP_QUAL_UNQUALIFIED_TYPE")
    private String unqualifiedType;

    @Column(name = "MEMO_",columnDefinition="TEXT")
    @Schema(description = "备注")
    private String memo;

    @Column(name = "SCHEME_CODE_")
    @Schema(description = "检测方案编号")//mes报工产生质检单对应的质检方案编码
    private String schemeCode;

    @Transient
    @Schema(description = "样本明细列表")
    private List<InspectionBillSimple> inspectionBillSimpleList;

    public InspectionBill(ManufactureWorkOrderTask orderTask) {
        this.busiType = InspectionEnums.ISBusiType.Process.getCode();
        this.checkStatus = InspectionEnums.CheckStatus.PLAN.getCode();//计划
        this.originWorkOrderTaskCode = orderTask.getTaskCode();
        this.originBizNo = orderTask.getBizNo();
        this.itemCode = orderTask.getItemCode();
        this.itemName = orderTask.getItemName();
        this.itemSpec = orderTask.getItemSpec();
        this.unitCode = orderTask.getUnitCode();
        this.quantity = new BigDecimal(orderTask.getTaskNum()+"");
        //this.checkQuantity = new BigDecimal(orderTask.getTaskNum()+"");
    }

    public InspectionBill(ManufactureWorkOrder workOrder) {
        this.busiType = InspectionEnums.ISBusiType.Process.getCode();
        this.checkStatus = InspectionEnums.CheckStatus.PLAN.getCode();//计划
        this.originWorkOrderCode = workOrder.getBatchCode();
        this.originBizNo = workOrder.getBizNo();
        this.quantity = new BigDecimal(workOrder.getBatchCompNum()+"");//取最后工序任务完工数量
        //this.checkQuantity = new BigDecimal(workOrder.getBatchCompNum()+"");
    }

    @Override
    public String initTypeCode() {
        return "QC1";
    }


}
